package casa.depesos.app.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.ActivityWorkBinding;
import casa.depesos.app.com.frag.EmptyFragment;
import casa.depesos.app.com.frag.NetFailFragment;
import casa.depesos.app.com.http.BaseBean;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.WorkBean;
import casa.depesos.app.com.http.data.Work;
import casa.depesos.app.com.http.req.SaveWorkReq;
import casa.depesos.app.com.http.req.WorkReq;
import casa.depesos.app.com.it.TextChangeListener;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class WorkActivity extends BaseInfoActivity {

    ActivityWorkBinding binding;
    Work work;
    List<String> jobs;

    @Override
    public void getData() {
        showLoading();
        clearFragment(getSupportFragmentManager());
        new WorkReq().req(new ReqCallback<WorkBean>() {
            @Override
            public void callback(boolean success, WorkBean bean) {
                hideLoading();
                int type;
                if (success && bean.isValid()) {
                    type = 0;
                    work = bean.work;
                    work.option = 1;
                } else if (bean != null) {
                    type = 1;
                    work = new Work();
                    work.option = 0;
                    work.type = 1;
                    work.exp = 6;
                    work.income = 3;
                    work.job = 1;
                } else {
                    type = 2;
                }
                init(type);
            }
        });
    }

    final String title = "Información laboral";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWorkBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (Constant.white || fromMe()) {
            if (fromMe()) {
                binding.bottom.setVisibility(View.GONE);
                binding.bg.setBackgroundResource(R.drawable.login_bg);

                int n = binding.linear.getChildCount();
                for (int i = 0; i < n; i++) {
                    View v = binding.linear.getChildAt(i);
                    v.setBackgroundResource(com.date.picker.R.drawable.white_round_bg2);
                }
                List<TextView> tmp = new ArrayList<>();
                tmp.add(binding.work);
                tmp.add(binding.job);
                tmp.add(binding.earn);
                tmp.add(binding.exp);
                for (TextView v : tmp) {
                    v.setBackground(null);
                    v.setPadding(0, 0, 0, 0);
                    v.setTextSize(15);
                    v.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                }
            } else {
                int n = binding.linear.getChildCount();
                int p = Utils.dp2Px(this, 15);
                for (int i = 0; i < n; i++) {
                    View v = binding.linear.getChildAt(i);
                    v.setPadding(p, p, p, 0);
                    ((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin = 0;
                }
            }
            binding.top1.getRoot().setVisibility(View.GONE);
            binding.top2.getRoot().setVisibility(View.VISIBLE);
            binding.top2.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
            binding.top2.back.setEnabled(true);
            binding.top2.back.setOnClickListener(view -> finish());
            binding.top2.title.setText(title);

            binding.getRoot().setBackgroundResource(R.color.bg2);
        } else {
            int n = binding.linear.getChildCount();
            int p = Utils.dp2Px(this, 15);
            for (int i = 0; i < n; i++) {
                View v = binding.linear.getChildAt(i);
                v.setPadding(p, p, p, 0);
                ((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin = 0;
            }
            initTop(binding.top1);
            binding.top2.getRoot().setVisibility(View.GONE);
        }
        Report.show("ACTION_JJ_001");
        getData();
    }

    private void init(int type) {
        fail = false;
        if (type == 2) {
            fail = true;
            replaceFragment(R.id.frame, new NetFailFragment().setTitle(title));
            return;
        }
        if (type == 1 && fromMe()) {
            replaceFragment(R.id.frame, new EmptyFragment().setTitle(title));
            return;
        }

        binding.work.setListener(new TextChangeListener() {
            @Override
            public void onTextChange(String txt, int index) {
                setJob(index, 0);
            }
        });

        int p = work.type - 1;
        binding.work.setText(getValue(R.array.work, p));
        if (work.job > 0) {
            setJob(p + 1, work.job);
        } else {
            binding.work.onTextChange(p + 1);
        }

        binding.exp.setText(getValue(R.array.exp, work.exp - 1));
        binding.earn.setText(getValue(R.array.earn, work.income - 1));

        chooseValue(binding.work, R.array.work, work, "type", "Tipo de empleo");
        chooseValue(binding.exp, R.array.exp, work, "exp", "Experiencia laboral");
        chooseValue(binding.earn, R.array.earn, work, "income", "Ingresos mensuales");

        binding.next.setOnClickListener(view -> next());
    }

    private void setJob(int index, int jobIndex) {
        switch (index) {
            case 1:
            case 2:
                jobs = Arrays.asList(getResources().getStringArray(R.array.full_value));
                binding.jobLayout.setVisibility(View.VISIBLE);
                if (jobIndex <= 0) {
                    jobIndex = 1;
                }
                binding.job.setText(jobs.get(jobIndex - 1));
                work.job = jobIndex;
                chooseValue(binding.job, jobs, work, "job", "Puesto", 0);
                break;
            case 3:
                jobs = Arrays.asList(getResources().getStringArray(R.array.individual_value));
                binding.jobLayout.setVisibility(View.VISIBLE);
                jobIndex -= 30;
                if (jobIndex <= 0) {
                    jobIndex = 1;
                }
                binding.job.setText(jobs.get(jobIndex - 1));
                work.job = jobIndex + 30;
                chooseValue(binding.job, jobs, work, "job", "Puesto", 30);
                break;
            default:
                jobs = null;
                work.job = 0;
                binding.jobLayout.setVisibility(View.GONE);
        }
    }

    private void next() {
        showLoading();
        Report.click("ACTION_JJ_003");
        new SaveWorkReq(work).req(new ReqCallback() {
            @Override
            public void callback(boolean success, BaseBean bean) {
                hideLoading();
                if (success || Constant.white) {
                    if (getIntent().getIntExtra("from", 0) == 1) {
                        finish();
                        return;
                    }
                    if (Constant.white) {
                        Utils.setStatus(25);
                    }
                    Intent i = new Intent(WorkActivity.this, TouchActivity.class);
                    i.putExtras(getIntent());
                    startActivity(i);
                    finish();
                }
            }
        });
    }

    @Override
    protected void check() {

    }

    @Override
    protected int getIndex() {
        return 1;
    }
}
